package com.ruoyi.web.service.impl;

import com.ruoyi.system.service.impl.SysUserServiceImpl;
import com.ruoyi.web.domain.OperatorRecord;
import com.ruoyi.web.domain.OrderRecord;
import com.ruoyi.web.domain.Product;
import com.ruoyi.web.domain.RefundRecord;
import com.ruoyi.web.mapper.OperatorRecordMapper;
import com.ruoyi.web.mapper.ProductMapper;
import com.ruoyi.web.mapper.RefundRecordMapper;
import com.ruoyi.web.service.IOrderRecordService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;


/**
 * 商品Service业务层处理
 *
 * @author song
 * @date 2024-05-11
 */
@Service
public class OrderRecordServiceImpl implements IOrderRecordService {
    private static final Logger log = LoggerFactory.getLogger(SysUserServiceImpl.class);

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private RefundRecordMapper refundRecordMapper;

    @Autowired
    private OperatorRecordMapper operatorRecordMapper;

    /**
     * 根据订单编号查询订单
     *
     * @param productNo 订单编号
     * @return
     */

    //TODO 未考虑手机号查询
    @Override
    public OrderRecord selectProductByNo(String productNo) {

        OrderRecord orderRecord = new OrderRecord();
        Product product = productMapper.selectProductByNo(productNo);

        OperatorRecord operatorRecord = operatorRecordMapper.selectOperatorRecordByProductNo(productNo);

        RefundRecord refundRecord = refundRecordMapper.selectRefundRecordByProductNo(productNo);

//        基本数填充
        if (!Objects.isNull(product)) {
            orderRecord.setName(product.getProductName());
            orderRecord.setProductNo(product.getProductNo());
            orderRecord.setProduct(product);
        } else if (!Objects.isNull(operatorRecord)) {
            orderRecord.setName(operatorRecord.getProductName());
            orderRecord.setProductNo(operatorRecord.getProductNo());
        } else if (!Objects.isNull(refundRecord)) {
            orderRecord.setName(refundRecord.getProductName());
            orderRecord.setProductNo(refundRecord.getProductNo());
        }
//        派单数据填充
        isRecordEmpty(orderRecord, refundRecord, operatorRecord);

        return orderRecord;
    }

    /**
     * 查询订单列表
     *
     * @param product 订单信息
     * @return
     *
     */

    @Override
    public List<OrderRecord> selectOrderRecordList(Product product) {
    List<OrderRecord> orderRecords = new ArrayList<>();


    List<Product> products = productMapper.selectProductList(product);

    if (products.isEmpty() && product.getProductNo() != null) {
        OrderRecord orderRecord = new OrderRecord();

        orderRecord.setProductNo(product.getProductNo());

        RefundRecord refundRecord = refundRecordMapper.selectRefundRecordByProductNo(product.getProductNo());
        OperatorRecord operatorRecord = operatorRecordMapper.selectOperatorRecordByProductNo(product.getProductNo());

        String name= operatorRecord==null ? refundRecord==null ? null : refundRecord.getProductName() : operatorRecord.getProductName();
        orderRecord.setName(name);

        isRecordEmpty(orderRecord, refundRecord, operatorRecord);
        orderRecords.add(orderRecord);
    }

    products.forEach(product1 -> {
        OrderRecord orderRecord = new OrderRecord();
        orderRecord.setProductName(product1.getProductName());
        orderRecord.setName(product1.getProductName());
        orderRecord.setProduct(product1);
        orderRecord.setProductNo(product1.getProductNo());
        RefundRecord refundRecord = refundRecordMapper.selectRefundRecordByProductNo(product1.getProductNo());
        OperatorRecord operatorRecord = operatorRecordMapper.selectOperatorRecordByProductNo(product1.getProductNo());
        isRecordEmpty(orderRecord, refundRecord, operatorRecord);

        orderRecords.add(orderRecord);
    });

    return orderRecords;
}

    private void isRecordEmpty(OrderRecord orderRecord, RefundRecord refundRecord, OperatorRecord operatorRecord) {
        if (operatorRecord != null) {

            orderRecord.setOperatorStatus("已派单");
            orderRecord.setOperatorRecord(operatorRecord);
        } else {
            orderRecord.setOperatorStatus("未派单");
        }

        if (refundRecord != null) {
            orderRecord.setRefundStatus("已退款");
            orderRecord.setRefundRecord(refundRecord);
        } else {
            orderRecord.setRefundStatus("未退款");
        }
    }

}
